const {userModel} = require('../utils/db');

// 查询
exports.getUser = info => {
    return new Promise((resolve , reject) => {
        userModel.findOne(info).then(data => {
            // console.log(data)
            resolve({code : 1 , msg : '查询成功' , data})
        })
        .catch(err => {
            resolve({code : 0 , msg : '数据库错误'})
        })
    })
}


// 插入
exports.addData = (info) => {
    return new Promise((resolve , reject) => {
        new userModel(info).save()
            .then(data => resolve({code : 1 , msg : '插入成功'}))
            .catch(err => resolve({code : 0 , msg : '数据库错误'}))
    })
}


// 更新
exports.updateData = (oldInfo , newInfo) => {
    return new Promise(resolve => {
        userModel.updateOne(oldInfo , newInfo)
            .then(data => resolve({code : 1 , msg : '修改成功'}))
            .catch(err => resolve({code : 0 , msg : '数据库错误'}))
    })
}



// 删除
exports.delData = (info) => {
    return new Promise(resolve => {
        userModel.deleteOne(info)
        .then(data => resolve({code : 1 , msg : '删除成功'}))
        .catch(err => resolve({code : 0 , msg : '数据库错误'}))
    })
}
